﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.XProperty.Editors
{
    /// <summary>
    /// brush编辑器
    /// brush分为SolidColorBrush,
    /// GradientBrush,
    /// ImageBrush
    /// </summary>
    public partial class XBrushEditor : XPropertyValueEditor
    {
        public static DependencyProperty PropertyValueProperty = DependencyProperty.Register("PropertyValue", typeof(BrushEx), typeof(XBrushEditor), new PropertyMetadata(propertyChangedCallback));
        private static void propertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            XBrushEditor editor = d as XBrushEditor;
            if (e.NewValue != null)
            {
                editor.brush = (BrushEx)e.NewValue;
                editor._btn.Background = ((BrushEx)e.NewValue).Brush;
            }
            else
            {
                editor.brush = null;
                editor._btn.Background = null;
            }
        }
        protected override void OnXInit(Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyItem)
        {
            Binding binding = new Binding();
            binding.Source = propertyItem;
            binding.Path = new PropertyPath("Value");
            binding.Mode = BindingMode.TwoWay;
            this.SetBinding(PropertyValueProperty, binding);
        }
        protected override DependencyProperty GetPropertyValue()
        {
            return PropertyValueProperty;
        }
        private BrushEx brush;
        protected override void OnUnDo(object oldValue)
        {
            base.OnUnDo(oldValue);

            brush = (BrushEx)oldValue;
            _btn.Background = ((BrushEx)oldValue).Brush;
        }
        public XBrushEditor()
        {
            InitializeComponent();
        }

        protected override void OnInitXPropertyValue(XPropertyAttribute xProperty, System.Reflection.PropertyInfo propertyInfo, object value)
        {
        }
        protected override void OnXInit(Part partInstance, XPropertyAttribute xProperty, PropertyInfo propertyInfo, object value)
        {
            Binding binding = new Binding();
            binding.Source = partInstance;
            binding.Path = new PropertyPath(propertyInfo.Name);
            this.SetBinding(PropertyValueProperty, binding);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // 显示brush编辑窗口
            BrushEditWindow window = new BrushEditWindow(brush);
            if (window.ShowDialog() == true)
            {
                brush = window.ReturnBrush;
                _btn.Background = window.ReturnBrush == null ? null : window.ReturnBrush.Brush;
                this.UpdateXPropertyValue(window.ReturnBrush);
            }
        }
    }
}
